package com.xianrenit.xianrenpuzi.manager.controller;

import com.xianrenit.xianrenpuzi.manager.service.SysRoleService;
import com.xianrenit.xianrenpuzi.model.entity.sys.SysRole;
import com.xianrenit.xianrenpuzi.model.result.Result;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "角色管理模块")
//@CrossOrigin(originPatterns = "*" , allowCredentials = "true")
@RequestMapping("/admin/system/role")
public class SysRoleController {
    @Resource
    SysRoleService sysRoleService;
    @Operation(summary = "根据用户id查询所有角色和他已分配的角色列表")
    @GetMapping("allRoles/{userId}")
    public Result allRoles(@PathVariable("userId")Long userId){
        List<SysRole> sysRoleList = sysRoleService.allRoles(userId);
        return Result.ok(sysRoleList);
    }


    @Operation(summary = "根据id查询角色")
    @GetMapping("getById/{id}")
    public Result getById(@PathVariable("id")Long id){
        //删除时也可能前端需要得到删除的行数据
        SysRole sysRole = sysRoleService.getById(id);
        return Result.ok(sysRole);
    }
    @Operation(summary = "更新角色")
    @PutMapping("update")
    public Result update(@RequestBody SysRole sysRole){
        sysRoleService.update(sysRole);
        return Result.ok();
    }
    @Operation(summary = "新增角色")
    @PostMapping("save")
    public Result save(@RequestBody SysRole sysRole){
        sysRoleService.save(sysRole);
        return Result.ok();
    }

    @Operation(summary = "删除指定角色")
    @DeleteMapping("deleteById/{id}")
    public Result deleteById(@PathVariable("id")Long id){
        //删除时也可能前端需要得到删除的行数据
        sysRoleService.deleteById(id);
        return Result.ok();
    }
    @Operation(summary = "条件查询角色分页数据")
    @GetMapping("list/{pageNum}/{pageSize}")
    public Result listRoles(String roleName,
                            @PathVariable("pageNum") Integer pageNum ,
                            @PathVariable("pageSize")  Integer pageSize){
        PageInfo<SysRole> page = sysRoleService.listRoles(roleName,pageNum,pageSize);
        return Result.ok(page);
    }
}
